package com.hue.gulimail.gulimailproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hue.gulimail.gulimailproduct.entity.AttrEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hue.gulimail.gulimailproduct.entity.AttrGroupEntity;
import com.hue.gulimail.gulimailproduct.service.AttrGroupService;
import com.hue.common.utils.PageUtils;
import com.hue.common.utils.R;


/**
 * 属性分组
 *
 * @author yaofengqiao
 * @email 451737523@qq.com
 * @date 2021-08-12 16:24:48
 */
@RestController
@RequestMapping("gulimailproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("gulimailproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Integer catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimailproduct:attrgroup:list")
    public R list2(@RequestParam Map<String, Object> params,
                   @PathVariable("catelogId") Integer catelogId) {
        PageUtils page = attrGroupService.queryPage(params);
//        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("gulimailproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimailproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimailproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimailproduct:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取这个属性分组已经关联的所有基本属性
     *
     * @param attrgroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("gulimailproduct:attrgroup:delete")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrList = attrGroupService.getAttrRelation(attrgroupId);
        return R.ok().put("data", attrList);
    }

    /**
     * 获取这个属性分组没有关联的所有基本属性
     *
     * @param attrgroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    //@RequiresPermissions("gulimailproduct:attrgroup:delete")
    public R noAttrRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrGroupService.getNoAttrRelation(attrgroupId, params);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 添加属性和属性分组的关联
     * @param relations
     * @return
     */
    @PostMapping("/attr/relation")
    //@RequiresPermissions("gulimailproduct:attrgroup:delete")
    public R attrgroupAttrRelation(@RequestBody List<Map<String,Long>> relations) {
        attrGroupService.attrgroupAttrRelation(relations);
        return R.ok();
    }

    /**
     * 删除属性和属性分组的关联
     */
    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("gulimailproduct:attrgroup:delete")
    public R attrgroupAttrRelationDelete(@RequestBody List<Map<String,Long>> relations) {
        attrGroupService.attrgroupAttrRelationDelete(relations);
        return R.ok();
    }

    @GetMapping("/{catelogId}/withattr")
    public R catelogIdWithAttr(@PathVariable("catelogId")Long catelogId) {
        List<AttrGroupEntity> attrGroups = attrGroupService.catelogIdWithAttr(catelogId);
        return R.ok().put("data",attrGroups);
    }
}
